package org.mineground.commands.player;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mineground.Main;
import org.mineground.PermissionNodes;

/**
 * @file Spawn.java (01.03.2012)
 * @author Daniel Koenen
 *
 */
public class Spawn implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] arguments) {
        Player player = (Player) cs;
        World currentWorld = player.getWorld();
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_SPAWN_COMMAND)) {
            return true;
        }
        
        if (arguments.length > 0 && arguments[0].equalsIgnoreCase("set")) {
            if (!player.hasPermission(PermissionNodes.PERMISSION_SPAWN_CHANGE)) {
                return true;
            }
            
            Location playerLocation = player.getLocation();
            currentWorld.setSpawnLocation(playerLocation.getBlockX(), playerLocation.getBlockY(), playerLocation.getBlockZ());
            player.sendMessage(ChatColor.DARK_GREEN + "World spawn has been set.");
            return true;
        }
        
        if (currentWorld == Main.getInstance().getServer().getWorld(Main.getInstance().getConfigHandler().getSurvivalWorldName())) {
            Main.getInstance().getPlayer(player).initializeSurvivalMovementDelay(currentWorld.getSpawnLocation());
            return true;
        }
        
        player.teleport(currentWorld.getSpawnLocation());
        player.sendMessage(ChatColor.DARK_GREEN + "You've been warped to the spawn location.");
        return true;
    }
}
